home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 8 / IOPROG_8.ISO / soft / sdkplnet / mac / plgsk401.sit / PluginSDK 4.01a / Examples / Framework / CNetscapePlugin.cpp / CNetscapePlugin.cpp
Encoding:
C/C++ Source or Header  |  1996-08-06  |  12.4 KB  |  425 lines  |  [TEXT/CWIE]

  1. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. //
  3. // CNetscapePlugin.cpp
  4. //
  5. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  6. #include "CNetscapePlugin.h"
  7.  
  8. extern CNetscapePlugin* CreateNetscapePlugin( uint16 mode, CPluginArguments* adoptedArgs, NPSavedData* saved );
  9. extern NPError InitializeNetscapePlugin( void );
  10. extern void ShutdownNetscapePlugin( void );
  11. extern jref InitializeJavaClass( void );
  12.  
  13.  
  14. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  15. //
  16. //    Procedural Methods
  17. // 
  18. //    This are the methods called by npmac.cpp. NPP_New, NPP_SetWindow, NPP_Print,
  19. //  and NPP_HandleEvent are dispatched to a CNetscapePlugin object. NPP_NewStream calls the
  20. //  CNetscapePlugin object to create a CNetscapeStream object. NPP_WriteReady, NPP_Write,
  21. //  and NPP_DestroyStream are dispatched to that CNetscapeStream object.
  22. //
  23. //  Four external methods must be defined to initialize and shutdown the appropriate 
  24. //  classes: InitializeNetscapePlugin, CreateNetscapePlugin, ShutdownNetscapePlugin, 
  25. //  and InitializeJavaClass.
  26. // 
  27. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  28.  
  29.  
  30. //------------------------------------------------------------------------------------
  31. // NPP_Initialize:
  32. //------------------------------------------------------------------------------------
  33. NPError NPP_Initialize(void)
  34. {
  35.     NPError error = InitializeNetscapePlugin();
  36.     return error;
  37. }
  38.  
  39.  
  40. //------------------------------------------------------------------------------------
  41. // NPP_Shutdown:
  42. //------------------------------------------------------------------------------------
  43. void NPP_Shutdown(void)
  44. {
  45.     ShutdownNetscapePlugin();
  46. }
  47.  
  48.  
  49. //------------------------------------------------------------------------------------
  50. // NPP_New:
  51. //------------------------------------------------------------------------------------
  52. NPError NP_LOADDS
  53. NPP_New(NPMIMEType pluginType,
  54.                 NPP instance,
  55.                 uint16 mode,
  56.                 int16 argc,
  57.                 char* argn[],
  58.                 char* argv[],
  59.                 NPSavedData* saved)
  60. {
  61.     if (instance == NULL)
  62.         return NPERR_INVALID_INSTANCE_ERROR;
  63.                                                         // Had to perform the (const char *[]) casts
  64.                                                         // because MSVC++ is retarded.
  65.     CPluginArguments* theArgs = new CPluginArguments( argc, (const char **)argn, (const char **)argv );
  66.  
  67.     CNetscapePlugin* thePlugin = CreateNetscapePlugin( instance, mode, theArgs, saved );
  68.     
  69.     if( thePlugin == 0 )
  70.         return NPERR_OUT_OF_MEMORY_ERROR;
  71.     
  72.     return NPERR_NO_ERROR;
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. //------------------------------------------------------------------------------------
  80. // NPP_Destroy:
  81. //------------------------------------------------------------------------------------
  82. NPError NP_LOADDS
  83. NPP_Destroy(NPP instance, NPSavedData** save)
  84. {
  85.     if (instance == NULL)
  86.         return NPERR_INVALID_INSTANCE_ERROR;
  87.         
  88.     CNetscapePlugin* thePlugin = (CNetscapePlugin*) instance->pdata;
  89.     if (thePlugin == 0)
  90.         return NPERR_INVALID_PLUGIN_ERROR;
  91.         
  92.     NPError result = thePlugin->SaveData( save );
  93.     delete thePlugin;
  94.     
  95.     return result;
  96. }
  97.  
  98.  
  99.  
  100.  
  101. //------------------------------------------------------------------------------------
  102. // NPP_SetWindow:
  103. //------------------------------------------------------------------------------------
  104. NPError NP_LOADDS
  105. NPP_SetWindow(NPP instance, NPWindow* window)
  106. {
  107.     if (instance == NULL)
  108.         return NPERR_INVALID_INSTANCE_ERROR;
  109.  
  110.     CNetscapePlugin* thePlugin = (CNetscapePlugin*) instance->pdata;
  111.     if( thePlugin == 0 )
  112.         return NPERR_INVALID_PLUGIN_ERROR;
  113.     
  114.     return thePlugin->SetWindow( window );
  115. }
  116.  
  117.  
  118.  
  119. //------------------------------------------------------------------------------------
  120. // NPP_NewStream:
  121. //------------------------------------------------------------------------------------
  122. NPError NP_LOADDS
  123. NPP_NewStream(NPP instance,
  124.                             NPMIMEType type,
  125.                             NPStream *stream, 
  126.                             NPBool seekable,
  127.                             uint16 *stype)
  128. {
  129.     if (instance == NULL)
  130.         return NPERR_INVALID_INSTANCE_ERROR;
  131.                 
  132.     CNetscapePlugin* controller = (CNetscapePlugin*) instance->pdata;
  133.     
  134.     CNetscapeStream* theStream = controller->CreateStream(type, stream, seekable, *stype );
  135.     
  136.     if( theStream == 0 )
  137.         return NPERR_OUT_OF_MEMORY_ERROR;
  138.         
  139.     *stype = theStream->GetStreamType();
  140.     
  141.     return NPERR_NO_ERROR;
  142. }
  143.  
  144.  
  145. //------------------------------------------------------------------------------------
  146. // NPP_WriteReady:
  147. //------------------------------------------------------------------------------------
  148. int32 NP_LOADDS
  149. NPP_WriteReady(NPP instance, NPStream *stream)
  150. {
  151.     if (instance == NULL)
  152.         return -1;
  153.  
  154.     CNetscapeStream* theStream = CNetscapeStream::EvolveStream( stream );    
  155.     if( theStream == 0 )
  156.         return -1;
  157.     
  158.     return theStream->WriteReady(); 
  159. }
  160.  
  161.  
  162.  
  163. //------------------------------------------------------------------------------------
  164. // NPP_Write:
  165. //------------------------------------------------------------------------------------
  166. int32 NP_LOADDS
  167. NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
  168. {
  169.     if (instance == NULL)
  170.         return -1;
  171.     
  172.     CNetscapeStream* theStream = CNetscapeStream::EvolveStream( stream );    
  173.     if( theStream == 0 )
  174.         return -1;
  175.         
  176.     return theStream->Write( offset, len, buffer );
  177. }
  178.  
  179.  
  180.  
  181. //------------------------------------------------------------------------------------
  182. // NPP_DestroyStream:
  183. //------------------------------------------------------------------------------------
  184. NPError NP_LOADDS
  185. NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason)
  186. {
  187.     if (instance == NULL)
  188.         return NPERR_INVALID_INSTANCE_ERROR;
  189.         
  190.     CNetscapeStream* theStream = CNetscapeStream::EvolveStream( stream );
  191.     if( theStream == 0 )
  192.         return NPERR_GENERIC_ERROR;
  193.     
  194.     NPError retErr = theStream->Finish( reason );        
  195.     delete theStream;
  196.     
  197.     return retErr;
  198. }
  199.  
  200.  
  201. //------------------------------------------------------------------------------------
  202. // NPP_StreamAsFile:
  203. //------------------------------------------------------------------------------------
  204. void NP_LOADDS
  205. NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
  206. {
  207.     if (instance == NULL)
  208.         return;
  209.         
  210.     CNetscapeStream* theStream = CNetscapeStream::EvolveStream( stream );    
  211.     if( theStream == 0 )
  212.         return;
  213.  
  214.     theStream->StreamAsFile( fname );
  215. }
  216.  
  217.  
  218.  
  219. //------------------------------------------------------------------------------------
  220. // NPP_Print:
  221. //------------------------------------------------------------------------------------
  222. void NP_LOADDS
  223. NPP_Print(NPP instance, NPPrint* printInfo)
  224. {
  225.     if(printInfo == NULL)   // trap invalid parm
  226.         return;
  227.  
  228.     if (instance != NULL)
  229.     {
  230.         CNetscapePlugin* This = (CNetscapePlugin*) instance->pdata;
  231.         This->Print( printInfo );
  232.     }
  233.  
  234. }
  235.  
  236.  
  237. //------------------------------------------------------------------------------------
  238. // NPP_HandleEvent:
  239. // Mac-only.
  240. //------------------------------------------------------------------------------------
  241. int16 NPP_HandleEvent(NPP instance, void* event)
  242. {
  243.     NPBool eventHandled = FALSE;
  244.     if (instance == NULL)
  245.         return eventHandled;
  246.         
  247.     CNetscapePlugin* This = (CNetscapePlugin*) instance->pdata;
  248.     if( This )
  249.         eventHandled = This->Handle_Event( event );
  250.     
  251.     return eventHandled;
  252. }
  253.  
  254. //------------------------------------------------------------------------------------
  255. // NPP_URLNotify:
  256. //------------------------------------------------------------------------------------
  257. void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
  258. {
  259.     if( instance != NULL )
  260.     {
  261.         //
  262.     }
  263. }
  264.  
  265.  
  266. //------------------------------------------------------------------------------------
  267. // NPP_GetJavaClass:
  268. //------------------------------------------------------------------------------------
  269. jref NPP_GetJavaClass(void)
  270. {
  271.  
  272.     return InitializeJavaClass();
  273. }
  274.  
  275. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  276. //
  277. //    Object Allocation Methods
  278. //
  279. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  280.  
  281. //======================================================================
  282. //        CNetscapePlugin::CNetscapePlugin()
  283. //======================================================================
  284.  
  285. CNetscapePlugin::CNetscapePlugin( NPP instance, uint16 mode, CPluginArguments* adoptedArgs )
  286. :    mMode( mode ),
  287.     mArgs( adoptedArgs ),
  288.     mView( 0 )
  289. {
  290.     mNavigator = new CNavigator( instance );
  291.     instance->pdata = (void*) this;
  292. }
  293.  
  294. //======================================================================
  295. //        CNetscapePlugin::~CNetscapePlugin()
  296. //======================================================================
  297.  
  298. CNetscapePlugin::~CNetscapePlugin()
  299. {
  300.     delete mArgs;
  301.     delete mView;
  302.     delete mNavigator;
  303. }
  304.  
  305. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  306. //
  307. //    Object Interface Methods
  308. //
  309. //    These methods are called by the procedural methods above. They take care of basic housekeeping,
  310. //    such as keeping track of streams and windows, then notify any inherited classes through the
  311. //    virtual Handle_*() member functions.
  312. //
  313. //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  314.  
  315. //------------------------------------------------------------------------------------
  316. // CNetscapePlugin::SaveData
  317. //
  318. // Override this method to save critical plugin data in the NPSavedData structure. This
  319. // Data can then be restored in CreateNetscapePlugin.
  320. //------------------------------------------------------------------------------------
  321. NPError
  322. CNetscapePlugin::SaveData( NPSavedData** ) 
  323. {
  324.     return NPERR_NO_ERROR;
  325. }
  326.  
  327. //------------------------------------------------------------------------------------
  328. // CNetscapePlugin::SetWindow
  329. //
  330. // In general, you should not override CNetscapePlugin::SetWindow. Instead, you should
  331. // override the CPluginView::SetWindow method in your view class.
  332. //------------------------------------------------------------------------------------
  333. NPError    
  334. CNetscapePlugin::SetWindow( NPWindow* window )
  335. {    
  336.     if( mView == 0 )
  337.     {
  338.         mView = CreatePluginView( );
  339.     }
  340.     
  341.     if( mView == 0 ) {
  342.         return NPERR_OUT_OF_MEMORY_ERROR;
  343.     }
  344.     
  345.     NPError result = mView->SetWindow( window );
  346.     return result;
  347. }
  348.  
  349.  
  350. //------------------------------------------------------------------------------------
  351. // CNetscapePlugin::Print
  352. // 
  353. // In general, you should not override this method. Instead, override
  354. // CNetscapePlugin::PrintFull for full-page printing, and CtscapePlugin::PrintEmbeded
  355. // for embedded printing.
  356. //------------------------------------------------------------------------------------
  357. void
  358. CNetscapePlugin::Print( NPPrint* printInfo )
  359. {
  360.     if(printInfo == NULL)   // trap invalid parm
  361.         return;
  362.  
  363.     if (printInfo->mode == NP_FULL)
  364.     {
  365.         NPBool printResult = PrintFull( printInfo->print.fullPrint );
  366.         printInfo->print.fullPrint.pluginPrinted = printResult; // Do the default
  367.     }
  368.     else    // If not fullscreen, we must be embedded
  369.     {
  370.         PrintEmbeded( printInfo->print.embedPrint );
  371.     }
  372.  
  373. }
  374.  
  375. //------------------------------------------------------------------------------------
  376. // CNetscapePlugin::PrintFull
  377. //------------------------------------------------------------------------------------
  378. NPBool
  379. CNetscapePlugin::PrintFull( NPFullPrint& fullPrintInfo ) 
  380. {
  381.     return FALSE;
  382. }
  383.  
  384. //------------------------------------------------------------------------------------
  385. // CNetscapePlugin::PrintEmbeded
  386. //------------------------------------------------------------------------------------
  387. void
  388. CNetscapePlugin::PrintEmbeded( NPEmbedPrint& embedPrintInfo ) 
  389. {
  390. }
  391.  
  392. //------------------------------------------------------------------------------------
  393. // CNetscapePlugin::Handle_Event
  394. //
  395. // Only Mac plugins need override this method. Note that event can be cast to
  396. // (EventRecord*)
  397. //------------------------------------------------------------------------------------
  398. NPBool 
  399. CNetscapePlugin::Handle_Event( void* event)
  400. {
  401.     return FALSE;
  402. }
  403.  
  404.  
  405. //------------------------------------------------------------------------------------
  406. // CNetscapePlugin::CreateStream
  407. //
  408. // Override this method to associate new streams with your stream-handling classes.
  409. //------------------------------------------------------------------------------------
  410. CNetscapeStream*
  411. CNetscapePlugin::CreateStream( NPMIMEType type, NPStream *stream, NPBool seekable, uint16 stype )
  412. {
  413.     return new CNetscapeStream( type, stream, seekable, stype );
  414. }
  415.  
  416. //------------------------------------------------------------------------------------
  417. // CNetscapePlugin::CreatePluginView
  418. //
  419. // Override this method to associate your plugin with a view class.
  420. //------------------------------------------------------------------------------------
  421. CPluginView*
  422. CNetscapePlugin::CreatePluginView( )
  423. {
  424.     return new CPluginView( this );
  425. }